package com.bs.action;

import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;

import com.bs.bean.Person;
import com.bs.service.PersonService;

@SuppressWarnings("serial")
@ParentPackage("struts-default")
@Namespace("/person")
public class PersonAction extends ActionBase {

	private Person person;
	private PersonService personService;
	public Person getPerson() {
		return person;
	}
	public void setPerson(Person person) {
		this.person = person;
	}
	public PersonService getPersonService() {
		return personService;
	}
	public void setPersonService(PersonService personService) {
		this.personService = personService;
	}
	
	@Action("save")
	public void save() throws Exception{
		try {
			this.personService.save(person);
			response.getWriter().write(this.successInfo("添加员工信息成功"));
		} catch (Exception e) {
			response.getWriter().write(this.errorInfo("添加员工失败，可能是员工编号已存在"));
			e.printStackTrace();
		}
	}
	@Action("delete")
	public void batchDelete() throws Exception{
		try {
			String ids = (String) request.getParameter("ids");
			int rows = this.personService.batchDelete(ids);
			if(rows>=0){
				response.getWriter().write(this.successInfo("删除员工信息成功"));
			}
		} catch (Exception e) {
			response.getWriter().write(this.errorInfo("员工信息正在被使用，不可删除"));
			e.printStackTrace();
		}
	}
	
	@Action("findForPaging")
	public void findForPaging() throws Exception{
		try {
			int start = Integer.parseInt(request.getParameter("start"));
			int limit = Integer.parseInt(request.getParameter("limit"));
			List<Person> list = this.personService.findForPaging(start, limit);
			for(Person person : list){
				if(person.getManager()==null)
					person.setManager(new Person());
			}
			JSONArray persons = JSONArray.fromObject(list);
			int count = this.personService.totalCount();
			JSONObject obj = new JSONObject();
			obj.put("totalCount", count);
			obj.put("persons", persons);
			response.getWriter().write(obj.toString());
		} catch (Exception e) {
			response.getWriter().write(this.sysError());
			e.printStackTrace();
		}
	}
	
	@Action("getMaxId")
	public void getMaxId() throws Exception{
		try {
			int maxId = this.personService.getMaxId();
			response.getWriter().write("{status: true, id:"+maxId+"}");
		} catch (Exception e) {
			response.getWriter().write(this.sysError());
			e.printStackTrace();
		}
	}
	@Action("query")
	public void query() throws Exception{
		try {
			List<Person> list = this.personService.query(person);
			response.getWriter().write(JSONArray.fromObject(list).toString());
		} catch (Exception e) {
			response.getWriter().write(this.sysError());
			e.printStackTrace();
		}
	}
	
	@Action("update")
	public void update() throws Exception{
		try{
			int row = this.personService.update(this.person);
			if(row == 1){
				response.getWriter().write(this.successInfo("更新员工信息成功"));
			}else if(row == 0){
				response.getWriter().write(this.errorInfo("该员工记录不存在"));
			}
		} catch (Exception e) {
			response.getWriter().write(this.errorInfo("技术支持已存在"));
			e.printStackTrace();
		}
	}
}
